package com.narunas;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import android.content.Intent;
import android.os.Environment;
import android.util.Log;

public class ImageLoader {

	public WXdroid wxDroid;
	
	 public void viewFile(int file_nx){
	    	
	    	//InputStream is = img_loader.getURLStreamSet("PYAA10.gif", "");
	    		
	    		FTPClient ftp = new FTPClient();
	    	    boolean error = false;
	    	    try {
	    	      int reply;
	    	      ftp.connect("tgftp.nws.noaa.gov");
	    	      ftp.login("anonymous", "");

	    	      Log.d("WXDroid",  "Connected to tgftp.nws.noaa.gov.");
	    	    		
	    	      Log.d("WXDroid", ftp.getReplyString());

	    	      // After connection attempt, you should check the reply code to verify
	    	      // success.
	    	      reply = ftp.getReplyCode();
	    	      
	    	      if(!FTPReply.isPositiveCompletion(reply)) {
	    	        ftp.disconnect();
	    	        System.err.println("FTP server refused connection.");
	    	        System.exit(1);
	    	      }
	    	      // changing current directory to "fax/"
	    	      ftp.changeWorkingDirectory("fax");
	    	     
	    	      Log.d("WXDroid", "working directory: " + ftp.printWorkingDirectory());
	    	      
	    	      //FTPFile[] remote_file = ftp.listFiles("PYAA10.gif");
	    	      
	    	      ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
	    	      
	    	      
	    	        	      
	    	      // loading the file to a local folder
	    	      InputStream  img_file = ftp.retrieveFileStream("PYAA10.gif");
	    	      
	    	      /*
	    	      Intent intent = new Intent();
	    	      intent.setClass(WXdroid.this, showProgressFTP.class);
	    	        
	    	       startActivity(intent);
	    	      */
	    	      
	    	      
	    	      
		    	    String storage_state = Environment.getExternalStorageState();
		  			
		    	    if (storage_state.contains("mounted")) {
		  			
		  				String filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/NOAAWX/";
		  				File imageDirectory = new File(filepath);
				 		File local_file = new File(filepath + "PYAA10.gif");
				 		
				 		OutputStream out = new FileOutputStream(local_file);
				 		
						
						 byte[] buffer = new byte[1024];
						 int count;
						 
						 
						 while ((count = img_file.read(buffer)) !=  -1) {
							    if (Thread.interrupted() == true) {
							    	
							     String functionName = Thread.currentThread().getStackTrace()[2].getMethodName() + "()";
							     throw new InterruptedException("The function " + functionName + " was interrupted.");
							    
							    }
							   
							    out.write(buffer, 0, count);
							    
						 }
						 
						 
						 wxDroid.showImage();
						 
						 out.flush();
						 out.close();
						 img_file.close();
						 
						 Log.d("WXDroid", "file saved: "+ filepath + " " + local_file);
						 
			      
		  			} else {
		  				
		  				Log.d("WXDroid", "The SD card is not mounted");
		  				
		  			}
	    	      
	    	      
	    	     // ... // transfer files
	    	     ftp.logout();
	    	     ftp.disconnect();
	    	      
	    	    } catch(IOException e) {
	    	      error = true;
	    	      e.printStackTrace();
	    	    } catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
	    	      if(ftp.isConnected()) {
	    	        try {
	    	          ftp.disconnect();
	    	        } catch(IOException ioe) {
	    	          // do nothing
	    	        }
	    	      }
	    	     //System.exit(error ? 1 : 0);
	    	    }

	    	};
	    
	    
};
